package com.ruoyi.front.controller;

import com.ruoyi.admin.domain.ZhxComment;
import com.ruoyi.admin.domain.ZhxCommentTree;
import com.ruoyi.admin.service.IZhxCommentService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 评论管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/public/ZhxComment")
public class PubZhxCommentController extends BaseController
{
    @Autowired
    private IZhxCommentService zhxCommentService;

    /**
     *√获取评论列表
     *√获取今日新增评论列表
     *√查询评论列表(ByLike)
     */


    /**
     * 查询评论管理列表（未使用）
     */
    @PreAuthorize("@ss.hasPermi('ZhxAdmin:ZhxComment:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZhxComment zhxComment)
    {
        startPage();
        List<ZhxComment> list = zhxCommentService.selectZhxCommentList(zhxComment);
        return getDataTable(list);
    }

    /**
     * 导出评论管理列表（未使用）
     */
    @PreAuthorize("@ss.hasPermi('ZhxAdmin:ZhxComment:export')")
    @Log(title = "评论管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZhxComment zhxComment)
    {
        List<ZhxComment> list = zhxCommentService.selectZhxCommentList(zhxComment);
        ExcelUtil<ZhxComment> util = new ExcelUtil<ZhxComment>(ZhxComment.class);
        util.exportExcel(response, list, "评论管理数据");
    }

    /**
     * 获取评论管理详细信息（未使用）
     */
    @PreAuthorize("@ss.hasPermi('ZhxAdmin:ZhxComment:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        return success(zhxCommentService.selectZhxCommentByCommentId(commentId));
    }

    /**
     * 新增评论管理（未使用）
     */
    @PreAuthorize("@ss.hasPermi('ZhxAdmin:ZhxComment:add')")
    @Log(title = "评论管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZhxComment zhxComment)
    {
        return toAjax(zhxCommentService.insertZhxComment(zhxComment));
    }

    /**
     * 修改评论管理（未使用）
     */
    @PreAuthorize("@ss.hasPermi('ZhxAdmin:ZhxComment:edit')")
    @Log(title = "评论管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZhxComment zhxComment)
    {
        return toAjax(zhxCommentService.updateZhxComment(zhxComment));
    }

    /**
     * 删除评论管理（未使用）
     */
    @PreAuthorize("@ss.hasPermi('ZhxAdmin:ZhxComment:remove')")
    @Log(title = "评论管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(zhxCommentService.deleteZhxCommentByCommentIds(commentIds));
    }


    
    /**
     * 获取评论数量
     */
    @GetMapping("/commentNum")
    public Integer getCommentNum()
    {
        return zhxCommentService.getCommentNum();
    }

    /**
     * 获取今日新增评论数量
     */
    @GetMapping("/commentNumToday")
    public Integer getCommentNumToday()
    {
        return zhxCommentService.getCommentNumToday();
    }


    /**
     * 查询评论管理列表(ByLike)
     */
//    @PreAuthorize("@ss.hasPermi('ZhxAdmin:ZhxComment:list')")
    @GetMapping("/listByLike")
    public TableDataInfo listByLike(ZhxComment zhxComment)
    {
        startPage();
        List<ZhxCommentTree> list = zhxCommentService.selectZhxCommentListByLike(zhxComment);
        // 更新评论的回复数量
        ZhxComment zhxCommentTemp = new ZhxComment();
        zhxCommentTemp.setCommentId(zhxComment.getComBodyId());
        zhxCommentTemp.setCommentNum((long) list.size());
        zhxCommentService.updateZhxComment(zhxCommentTemp);

        return getDataTable(list);
    }

    /**
     * 新增评论管理
     */
//    @PreAuthorize("@ss.hasPermi('ZhxAdmin:ZhxComment:add')")
    @Log(title = "评论管理", businessType = BusinessType.INSERT)
    @PostMapping("/addComment")
    public AjaxResult addComment(@RequestBody ZhxComment zhxComment)
    {

//        zhxComment.setUserId(SecurityUtils.getUserId());
        int i = zhxCommentService.insertZhxCommentTodo(zhxComment);

        return toAjax(i);
    }


}
